﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Collections;
using Maticsoft.DBUtility;
using DQYT.HTDZ.Common;
using DQYT.HTDZ.Web;

namespace SkWeb.Handlers
{
    public class resultItem
    {
        private string jb;

        public string Jb
        {
            get { return jb; }
            set { jb = value; }
        }
        private string items;

        public string Items
        {
            get { return items; }
            set { items = value; }
        }

        //地层压力
        private string dcyl;

        public string DCYL
        {
            get { return dcyl; }
            set { dcyl = value; }
        }

        //井斜
        private string jx;
        public string JX
        {
            get { return jx; }
            set { jx = value; }
        }

        //施工方式
        private string sglx;

        public string SGLX
        {
            get { return sglx; }
            set { sglx = value; }
        }

        //夹层
        private string jc;
        public string JC
        {
            get { return jc; }
            set { jc = value; }
        }

        //返回标示，
        public string flag;
        public string FLAG
        {
            get { return flag; }
            set { flag = value; }
        }

        //返回tb_fa表中设计人
        public string user_name;
        public string USER_NAME
        {
            get { return user_name; }
            set { user_name = value; }
        }
        //提示信息,是否
        private string msg_hint="";

        public string Msg_hint
        {
            get { return msg_hint; }
            set { msg_hint = value; }
        }
        
    }
    public class Check
    {
        private bool is_hava;

        public bool Is_hava
        {
            get { return is_hava; }
            set { is_hava = value; }
        }
    }
    public partial class XiaocengJsHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string JH = Request.Params["name"];
            string ID = Request.Params["ID"];
            string resultJson;

            string strCheck = Request.QueryString["ischeck"];
            if (strCheck == "1")
            {
                Check cCheck = new Check();
                string strSql = @"select count(*) from tb_fa where word_doc is not null and  jing_id = "+ID;
                DataTable dt = GetList(strSql).Tables[0];
                if (Convert.ToInt32(dt.Rows[0][0]) > 0)
                    cCheck.Is_hava = true;
                else
                    cCheck.Is_hava = false;

                resultJson =   JsonHelper.ToJson(cCheck);
                Response.Write(resultJson);
                Response.End();
                return;
            }


            resultItem Item = new resultItem();
            string SQL = "";

            SQL = @"select * from pc_wellbore_zone_data t where WELL_COMMON_NAME='" + JH + "' order by PERFORATED_INT_TOP_BOUNDARY desc  ";
            DataTable dt2 = GetList(SQL).Tables[0];
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                //lbllist2.Text += dt2.Rows[i]["INTERBED"].ToString() + "<br />";
            }

            SQL = @"select * from pc_wellbore_zone_data t where interbed>=30.00 and WELL_COMMON_NAME='" + JH + "'";
            DataTable dt3 = GetList(SQL).Tables[0];
            //获取补孔井分级
           //////// lbllist2.Text = GetLVL(dt2);
            Item.Jb = GetLVL(dt2);
            float max = 0;
            float min = 0;
            //获取返回的数组
            if (dt2.Rows.Count > 0)
            {
                max = Convert.ToSingle(dt2.Rows[0]["PERFORATED_INT_TOP_BOUNDARY"]);
                min = Convert.ToSingle(dt2.Rows[dt2.Rows.Count - 1]["PERFORATED_INT_TOP_BOUNDARY"]);
            }
            string[] lists = GetList(max.ToString(), min.ToString(), dt3);
            string ss = "";
            for (int i = 0; i < dt3.Rows.Count+2; i++)
            {
                ss += lists[i];
            }
            string[] s = ss.Split(';');
            s = getArr(s);
            if (s[0] == "0" || s[1] == "0")
            {
                Item.flag = "0";
            }

            //对数组进行排序
            string[] qq = order(s);

            //输出排序后的结果
            string result = "";
            foreach (string item in qq)
            {
                //测试输出
                result += item + ";";
            }
            

            Item.Items = result;

            //获取地层压力
            string strSQL = "select t.*, t.rowid from skgysjb t where id='" + ID + "'";
            DataTable dt4 = GetList(strSQL).Tables[0];
            Item.DCYL = dt4.Rows[0]["DCYL"].ToString(); 
           // Item.JX = dt4.Rows[0]["JX"].ToString();
            Item.SGLX = dt4.Rows[0]["SGLX"].ToString();
            if (dt3.Rows.Count > 0)
            {
                Item.JC = "1";
            }

            SQL = @"select * from tb_fa where jing_id=" + ID;
            DataTable dt5 = GetList(SQL).Tables[0];
            if (dt5.Rows.Count > 0)
            {
                Item.USER_NAME = dt5.Rows[0]["SJR"].ToString() == Globals.CurrentUserInfo.USE_NAME ? "你" : dt5.Rows[0]["SJR"].ToString();
                DateTime sjrq = Convert.ToDateTime( dt5.Rows[0]["SJRQ"].ToString());
                TimeSpan span = DateTime.Now - sjrq;
                if (span.Hours > 1)
                    Item.Msg_hint = "设计时间已经超过1小时，可能已经放弃设计";

            }
            else
            {
                //Globals.CurrentUserInfo.USE_CODE
                  
                //获取设计人名和code插入到数据库
                SQL = "insert into tb_fa (jing_id,sjr,sjr_code,sjrq) values ('" + ID + "','" + Globals.CurrentUserInfo.USE_NAME + "','" + Globals.CurrentUserInfo.USE_CODE + "',sysdate)";
                DbHelperOra.ExecuteSql(SQL);
                //获取设计人名和code插入到数据库
            }

            resultJson = string.Empty;
            resultJson = "{\"success\":true}";
            resultJson=JsonHelper.ToJson(Item);
            Response.Write(resultJson);
            Response.End();
        ////    Label1.Text = result;


           
        }

        /// <summary>
        /// 补孔井分级
        /// </summary>
        /// <param name="dt">查询结果集</param>
        /// <returns>A-J</returns>
        public string GetLVL(DataTable dt)
        {
            //定义返回的级别
            string LVL = "";
            string Shu = "";
            //根据传入的datatable来找出30~60，60~120之间的数的个数。
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //判断夹层厚度是否为空
                if (!string.IsNullOrEmpty((dt.Rows[i]["INTERBED"].ToString())))
                {
                    //判断夹层厚度的值范围
                    if (double.Parse(dt.Rows[i]["INTERBED"].ToString()) >= 30 && double.Parse(dt.Rows[i]["INTERBED"].ToString()) <= 60)
                    {
                        Shu += dt.Rows[i]["INTERBED"].ToString() + ";";
                    }
                    //判断夹层厚度的值范围
                    if (double.Parse(dt.Rows[i]["INTERBED"].ToString()) >= 60 && double.Parse(dt.Rows[i]["INTERBED"].ToString()) <= 120)
                    {
                        Shu += dt.Rows[i]["INTERBED"].ToString() + ";";
                    }
                }
            }

            //根据“；”对字符串进行分组
            string[] shuzu = Shu.Split(';');
            shuzu = getArr(shuzu);

            if (shuzu.Length == 1)
            {
                ////返回A 夹层值在(30-60)之间
                if (double.Parse(shuzu[0]) >= 30 && double.Parse(shuzu[0]) <= 60)
                {
                    LVL = "A";
                }

                //返回B 夹层值在(60-120)之间
                if (double.Parse(shuzu[0]) >= 60 && double.Parse(shuzu[0]) <= 120)
                {
                    LVL = "B";
                }
              
            }

            if (shuzu.Length == 2)
            {
                //返回C 夹层值在(30-60)+(30-60)之间
                if ((double.Parse(shuzu[0]) >= 30 && double.Parse(shuzu[0]) <= 60) && (double.Parse(shuzu[1]) >= 30 && double.Parse(shuzu[1]) <= 60))
                {
                    LVL = "C";
                }
                //D (30-60)+ (60-120)
                if ((double.Parse(shuzu[0]) >= 30 && double.Parse(shuzu[0]) <= 60) && (double.Parse(shuzu[1]) >= 60 && double.Parse(shuzu[1]) <= 120))
                {
                    LVL = "D";
                }
                //E (60-120)+ (30-60)
                if ((double.Parse(shuzu[0]) >= 60 && double.Parse(shuzu[0]) <= 120) && (double.Parse(shuzu[1]) >= 30 && double.Parse(shuzu[1]) <= 60))
                {
                    LVL = "E";
                }
                //F (60-120)+ (60-120)
                if ((double.Parse(shuzu[0]) >= 60 && double.Parse(shuzu[0]) <= 120) && (double.Parse(shuzu[1]) >= 60 && double.Parse(shuzu[1]) <= 120))
                {
                    LVL = "F";
                }
            }
            if (shuzu.Length == 3)
            {
                //G (30-60)+ (30-60) + (30-60)
                if ((double.Parse(shuzu[0]) >= 30 && double.Parse(shuzu[0]) <= 60) && (double.Parse(shuzu[1]) >= 30 && double.Parse(shuzu[1]) <= 60) && (double.Parse(shuzu[2]) >= 30 && double.Parse(shuzu[2]) <= 60))
                {
                    LVL = "G";
                }
                //H (30-60)+ (60-120) + (30-60)
                if ((double.Parse(shuzu[0]) >= 30 && double.Parse(shuzu[0]) <= 60) && (double.Parse(shuzu[1]) >= 60 && double.Parse(shuzu[1]) <= 120) && (double.Parse(shuzu[2]) >= 30 && double.Parse(shuzu[2]) <= 60))
                {
                    LVL = "H";
                }
                //I (60-120)+ (30-60) + (30-60)
                if ((double.Parse(shuzu[0]) >= 60 && double.Parse(shuzu[0]) <= 120) && (double.Parse(shuzu[1]) >= 30 && double.Parse(shuzu[1]) <= 60) && (double.Parse(shuzu[2]) >= 30 && double.Parse(shuzu[2]) <= 60))
                {
                    LVL = "I";
                }
                //J (60-120)+ (60-120) + (30-60)
                if ((double.Parse(shuzu[0]) >= 60 && double.Parse(shuzu[0]) <= 120) && (double.Parse(shuzu[1]) >= 60 && double.Parse(shuzu[1]) <= 120) && (double.Parse(shuzu[2]) >= 30 && double.Parse(shuzu[2]) <= 60))
                {
                    LVL = "J";
                }
            }
            return LVL;
        }

        /// <summary>
        /// 获取数组
        /// </summary>
        /// <param name="dt">油顶，油底,查询结果集</param>
        /// <returns>数组</returns>
        public string[] GetList(string YD, string YD1, DataTable dt)
        {
            //定义数组用来返回
            int i = dt.Rows.Count;
            int s = 0;
            string[] list = new string[i + 2];

            //根据传入的datatable，排序给数组赋值
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                s++;
                list[j] += dt.Rows[j]["PERFORATED_INT_TOP_BOUNDARY"].ToString() + ";";
                list[j] += dt.Rows[j]["PERFORATED_INT_BOTTOM_BOUNDARY"].ToString() + ";";

            }
            list[i] = YD1 + ";";
            list[i + 1] = YD + ";";


            return list;
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strSql)
        {
            return DbHelperOra.Query(strSql.ToString());
        }

        //排序
        public string[] order(string[] ss)
        {

            double temp = 0;
            //临时交换变量
            //数据循环排序
            for (int i = 0; i < ss.Length - 1; i++)
            {
                for (int j = 0; j < ss.Length - i - 1; j++)
                {
                    if (Convert.ToDouble(ss[j]) > Convert.ToDouble(ss[j + 1]))
                    {
                        //两个数的交换                              
                        temp = Convert.ToDouble(ss[j]);
                        ss[j] = ss[j + 1];
                        ss[j + 1] = temp.ToString();
                    }
                }
            }
            return ss;

        }

        //去掉数组中的空项
        public string[] getArr(string[] s)
        {
            ArrayList al = new ArrayList();
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] != string.Empty)
                    al.Add(s[i]);
            }

            string[] str2 = new string[al.Count];
            for (int i = 0; i < str2.Length; i++)
            {
                str2[i] = (string)al[i];
            }
            return str2;
        }



    }
}